package org.jetbrains.plugins.scala.compiler.highlighting

import scala.util.matching.Regex

private object CompilerMessages {
  // Bsp message includes location
  // Example: "Not found: res1 [16:1]"
  private val bspMessageTemplate: Regex = """^(.*)(\s\[\d+:\d+\])$""".r

  def description(message: String): String = {
    val trimmedMessage = message.trim
    val last = lastLine(trimmedMessage)
    val suffix = last match {
      case bspMessageTemplate(_, position) => position
      case line => line
    }
    val noSuffix = trimmedMessage.stripSuffix(suffix).trim
    if (noSuffix.isBlank) trimmedMessage else noSuffix
  }

  def isUnusedImport(description: String): Boolean =
    description.trim.equalsIgnoreCase("unused import")

  def isNeedsToBeAbstract(description: String): Boolean = {
    val trimmedDescription = description.trim

    trimmedDescription.contains("needs to be abstract") ||
      trimmedDescription.startsWith("object creation impossible")
  }

  def isNoWarningsCanBeIncurred(description: String): Boolean =
    description.trim.equalsIgnoreCase("No warnings can be incurred under -Werror (or -Xfatal-warnings)")

  private def lastLine(text: String): String = {
    val lastLineSeparator = text.lastIndexOf('\n')
    if (lastLineSeparator != -1)
      text.substring(lastLineSeparator).trim
    else
      text
  }
}
